﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GradeApp.Web;

namespace GradeApp.Views
{
    public partial class Schedule : Page
    {
        public Schedule()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            classesDomainDataSource.FilterDescriptors.Add(
                new FilterDescriptor
                    {
                        PropertyPath = "CourseId",
                        Operator = FilterOperator.IsEqualTo,
                        Value = GradeAppContext.CurrentCourseId
                    });
        }

        private void classesDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void Form_EditEnded(object sender, DataFormEditEndedEventArgs e)
        {
            var currentItem = Form.CurrentItem as Classes;
            currentItem.Id = Guid.NewGuid();
            currentItem.CourseId = GradeAppContext.CurrentCourseId;
            
            Form.CommitEdit();
            classesDomainDataSource.SubmitChanges();
        }

        private void Form_DeletingItem(object sender, CancelEventArgs e)
        {
            var currentItem = Form.CurrentItem;
        }
    }
}
